package com.xyht.sca_s.student_manage_system.modules.assetConsumable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.SmsAssetConsumableStorageManage;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.req.SmsAssetConsumableStorageManageReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.resp.SmsAssetConsumableStorageManageResp;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.mapper.SmsAssetConsumableStorageManageMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.service.SmsAssetConsumableStorageManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 仓库管理表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-09-22
 */
@Service
public class SmsAssetConsumableStorageManageServiceImpl extends ServiceImpl<SmsAssetConsumableStorageManageMapper, SmsAssetConsumableStorageManage> implements SmsAssetConsumableStorageManageService {

    @Resource
    private SmsAssetConsumableStorageManageMapper storageManageMapper;
    @Resource
    private CacheUtil cacheUtil;

    @Override
    public ResponseResult addStorageManage(SmsAssetConsumableStorageManageReq req) {
        if (isNullOrEmpty(req.getStorageManager(), req.getStorageName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //名称不能重复
        Integer count = storageManageMapper.selectCount(new LambdaQueryWrapper<SmsAssetConsumableStorageManage>()
                .eq(SmsAssetConsumableStorageManage::getStorageName, req.getStorageName()));
        if (count > 0) {
            return CommonResult.failed(CommonCodeEnum.STORAGE_MANAGE_NAME_REPEAT);
        }
        SmsAssetConsumableStorageManage storageManage = new SmsAssetConsumableStorageManage();
        storageManage.setStorageManager(req.getStorageManager());
        storageManage.setStorageName(req.getStorageName());
        storageManageMapper.insert(storageManage);
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateStorageManage(SmsAssetConsumableStorageManageReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(), updateReq.getStorageManager(), updateReq.getStorageName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsAssetConsumableStorageManage smsAssetConsumableStorageManage = storageManageMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(smsAssetConsumableStorageManage)) {
            return CommonResult.failed(CommonCodeEnum.STORAGE_MANAGE_NOT_EXIST);
        }
        //名称不能重复
        Integer count = storageManageMapper.selectCount(new LambdaQueryWrapper<SmsAssetConsumableStorageManage>()
                .eq(SmsAssetConsumableStorageManage::getStorageName, updateReq.getStorageName())
                .ne(SmsAssetConsumableStorageManage::getId, updateReq.getId()));
        if (count > 0) {
            return CommonResult.failed(CommonCodeEnum.STORAGE_MANAGE_NAME_REPEAT);
        }
        smsAssetConsumableStorageManage.setStorageManager(updateReq.getStorageManager());
        smsAssetConsumableStorageManage.setStorageName(updateReq.getStorageName());
        storageManageMapper.updateById(smsAssetConsumableStorageManage);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedStorageManage(SmsAssetConsumableStorageManageReq deleteReq) {
        if (isNullOrEmpty(deleteReq.getDeletedStorageIdList()) || deleteReq.getDeletedStorageIdList().size() < 1) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        storageManageMapper.deleteBatchIds(deleteReq.getDeletedStorageIdList());
        return CommonResult.success();
    }

    @Override
    public ResponseResult getStorageManageList(Integer pageSize, Integer pageNum, String storageName) {
        Page<SmsAssetConsumableStorageManage> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsAssetConsumableStorageManage> storageManageLambdaQueryWrapper =
                new LambdaQueryWrapper<SmsAssetConsumableStorageManage>()
                        .orderByDesc(SmsAssetConsumableStorageManage::getCreateTime);
        if (!isNullOrEmpty(storageName)) {
            storageManageLambdaQueryWrapper.like(SmsAssetConsumableStorageManage::getStorageName, storageName);
        }
        storageManageMapper.selectPage(page, storageManageLambdaQueryWrapper);
        List<SmsAssetConsumableStorageManageResp> list = page.getRecords()
                .stream()
                .map(smsAssetConsumableStorageManage -> {
                    SmsAssetConsumableStorageManageResp resp = new SmsAssetConsumableStorageManageResp();
                    SmsSearchUserResp storageManagerInfo = new SmsSearchUserResp();

                    SmsUser userInfo = cacheUtil.getUserInfo(smsAssetConsumableStorageManage.getStorageManager());
                    if (userInfo != null) {
                        BeanUtils.copyProperties(userInfo, storageManagerInfo);
                    }

                    resp.setId(smsAssetConsumableStorageManage.getId());
                    resp.setStorageName(smsAssetConsumableStorageManage.getStorageName());
                    resp.setStorageManagerUserInfo(storageManagerInfo);
                    return resp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(list,(int)page.getTotal());
    }
}
